/// this class was used to import data from dasBlog xml files
/// to my blog's DB.
/// I use the AbstractModelTestCase simply as a quick and dirty way to
/// gain access to a configures ActiveRecord session.


using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.IO;
using System.Xml;

namespace KenEgozi.Com.Domain.Tests
{
	/*
	[TestFixture]
	public class dasBlogMigration : AbstractModelTestCase
	{
		[Test]
		public void ImportPosts()
		{
			//ExecuteInTransaction(delegate
			//{
				string path = @"C:\Dev\Solutions\Blog\Old\content";
				string[] fileNames = Directory.GetFiles(path, "*.dayentry.xml");

				int i = 0;
				Author author = Author.FindAll()[0];
				Blog.Blog blog = author.Blogs[0];

				Dictionary<string, Tag> tags = new Dictionary<string, Tag>();

				foreach (string fileName in fileNames)
				{
					XmlTextReader reader = new XmlTextReader(fileName);
					while (reader.ReadToFollowing("Entry"))
					{
						ProcessPost(reader, author, blog, tags);
						++i;
					}
					reader.Close();
				}
				Console.WriteLine(i);
			//});
		}

		[Test]
		public void ImportComments()
		{
			//ExecuteInTransaction(delegate
			//{
			string path = @"C:\Dev\Solutions\Blog\Old\content";
			string fileName = Directory.GetFiles(path, "AllComments.xml")[0];

			int i = 0;


			Dictionary<string, RedirectionEntry> entries = new Dictionary<string, RedirectionEntry>();
			foreach (RedirectionEntry entry in RedirectionEntry.FindAll())
				entries.Add(entry.EntryId, entry);

			Dictionary<string, Tag> tags = new Dictionary<string, Tag>();

			XmlTextReader reader = new XmlTextReader(fileName);
			while (reader.ReadToFollowing("Comment"))
			{
				ProcessComment(reader, entries);
				++i;
			}
			reader.Close();
			Console.WriteLine(i);
			//});
		}

		private void ProcessPost(XmlTextReader reader, Author author, Blog.Blog blog, Dictionary<string, Tag> tags)
		{
			Post post = new Post();
			post.Author = author;
			post.Blog = blog;
			reader.ReadToFollowing("Content");
			post.Content = reader.ReadString();
			reader.ReadToFollowing("Created");
			post.SetCreationDate(reader.ReadElementContentAsDateTime().ToUniversalTime());
			reader.ReadToFollowing("Modified");
			post.SetUpdateDate(reader.ReadElementContentAsDateTime().ToUniversalTime());
			reader.ReadToFollowing("EntryId");
			string entryId = reader.ReadString();
			reader.ReadToFollowing("Title");
			post.Title = reader.ReadString();
			reader.ReadToFollowing("Categories");
			string categoriesText = reader.ReadString();
			foreach (string categoryName in categoriesText.Split(new char[]{ ';', '|'},StringSplitOptions.RemoveEmptyEntries))
			{
				if (!tags.ContainsKey(categoryName))
				{
					Tag tag = new Tag();
					tag.Name = categoryName;
					tag.Create();
					tags.Add(tag.Name, tag);
				}
				if (!post.Tags.Contains(tags[categoryName]))
					post.Tags.Add(tags[categoryName]);
			}
			post.CreatePermalink();
			post.Create();
			RedirectionEntry redirectionEntry = new RedirectionEntry();
			redirectionEntry.PostId = post.Id;
			redirectionEntry.EntryId = entryId;
			redirectionEntry.Create();
		}
		private void ProcessComment(XmlTextReader reader, Dictionary<string, RedirectionEntry> entries)
		{
			Comment comment = new Comment();
			reader.ReadToFollowing("Content");
			comment.Content = reader.ReadString();
			reader.ReadToFollowing("Created");
			comment.SetCreationDate(reader.ReadElementContentAsDateTime().ToUniversalTime());
			reader.ReadToFollowing("Modified");
			comment.SetUpdateDate(reader.ReadElementContentAsDateTime().ToUniversalTime());
			reader.ReadToFollowing("TargetEntryId");
			string entryId = reader.ReadString();
			reader.ReadToFollowing("Author");
			comment.Name = reader.ReadString();
			reader.ReadToFollowing("AuthorEmail");
			comment.Email = reader.ReadString();
			reader.ReadToFollowing("AuthorHomepage");
			comment.Homepage = reader.ReadString();
			reader.ReadToFollowing("AuthorIPAddress");
			comment.IpAddress = reader.ReadString();

			comment.Post = Post.Find(entries[entryId.ToLower()].PostId);
			comment.Create();
		}
	}
	 * */
}
